/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.uvsq.isty.bdh.hchess.gui.menu;

import fr.uvsq.isty.bdh.hchess.engine.Engines;
import fr.uvsq.isty.bdh.hchess.engine.HumanPlayer;
import fr.uvsq.isty.bdh.hchess.gui.MainWindow;
import fr.uvsq.isty.bdh.hchess.interfacing.IChessPlayer;
import fr.uvsq.isty.bdh.hchess.ranking.Player;
import fr.uvsq.isty.bdh.hchess.ranking.Players;
import fr.uvsq.isty.bdh.hchess.uci.UciChessPlayer;
import java.awt.Component;
import java.io.IOException;
import javax.swing.*;
import java.awt.Color;


/**
 *
 * @author Clement Bellot <clementbellot@gmail.com>
 */
public class PlayerOrIASelectFrame extends JEmptyPanel{
    private MainWindow mainWindow;
    
    private JLabel pannelName;
    
    private ButtonGroup playerTypeSelect;
    
    private JRadioButton aiSelectButton;
    private JComboBox aiSelectCombo;
    
    private JRadioButton humanPlayerSelectButton;
    private JComboBox humanPlayerSelectCombo;
    
    private boolean playerIsBlack;
    public PlayerOrIASelectFrame(MainWindow mainWindow, boolean isBlack){
        this.mainWindow = mainWindow;
        
        playerIsBlack = isBlack;
        playerTypeSelect = new ButtonGroup();
        aiSelectButton = new JRadioButton("Ordi");
        humanPlayerSelectButton = new JRadioButton("Sapiens");
        humanPlayerSelectCombo = new JComboBox();
        if(playerIsBlack){
            pannelName = new JLabel("Joueur Noir:");
            pannelName.setForeground(Color.WHITE);
        }else{
            pannelName = new JLabel("Joueur Blanc:");
            pannelName.setForeground(Color.WHITE);
        }
        
        
        // Filling da choice (humans).
        try {
            for( Player P: Players.getInstance().getItemsWithFilter("") ) {
                humanPlayerSelectCombo.addItem(P.getName());
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        // Filling da choice (players).
        aiSelectCombo = new JComboBox();
        try {
            for( String S: Engines.getInstance().list() ) {
                aiSelectCombo.addItem(S);
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        playerTypeSelect.add(humanPlayerSelectButton);
        playerTypeSelect.add(aiSelectButton);
        
        pannelName.setAlignmentX(Component.CENTER_ALIGNMENT);
        this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));	
        this.add(pannelName);
        
        humanPlayerSelectButton.setAlignmentX(Component.CENTER_ALIGNMENT);		
        this.add(humanPlayerSelectButton);
        
        this.add(humanPlayerSelectCombo);

        aiSelectButton.setAlignmentX(Component.CENTER_ALIGNMENT);		
        this.add(aiSelectButton);
		
        this.add(aiSelectCombo);
        
        humanPlayerSelectButton.setSelected(true);
    }
    
    public IChessPlayer getPlayer(){
        IChessPlayer player;
        if(humanPlayerSelectButton.isSelected())
        {
            String name;
            if(humanPlayerSelectCombo.getSelectedItem() == null) {
                name = "Guest";
            } else {
                name = humanPlayerSelectCombo.getSelectedItem().toString();
            }
            player = new HumanPlayer(mainWindow.getGameData(), name);
        } else {
            if(  aiSelectCombo.getSelectedItem() == null)
            {player = null;}
            else
            {
                try {
                    player = new UciChessPlayer(
                            Engines.getInstance().getEnginePath( aiSelectCombo.getSelectedItem().toString() ),
                            playerIsBlack);
                } catch (IOException ex) {
                    assert false : "Failed to open the engine.";
                    player = null;
                }
            }
        }
        return player;
    }
}
